package org.example.thread.testReturn.way2;

import java.util.concurrent.*;

/**
 * @author liyishan
 * @date 2023/8/3 13:28
 * @apiNote
 */

public class FutureTest {
    public static void main(String[] args)throws ExecutionException,InterruptedException {
        test3();
    }

    private static void test ()throws ExecutionException,InterruptedException{
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<String> future = executorService.submit(new Callable<String>(){
            @Override
            public String call() throws Exception {
                return "测试异步结果";
            }
        });
        System.out.println(future.get());
        executorService.shutdown();
    }

    private static void test2 ()throws ExecutionException,InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>(){
            @Override
            public String call() throws Exception {
                return "测试异步结果";
            }
        });
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }

    private static void test3()throws  ExecutionException,InterruptedException{
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>(){
            @Override
            public String call() throws Exception {
                return "测试异步结果";
            }
        });
        executorService.execute(futureTask);
        System.out.println(futureTask.get());
        executorService.shutdown();
    }
}
